<?php

namespace app\openapi;

use think\exception\Handle;
use think\Response;
use Throwable;

class Exception extends Handle
{
    public $httpStatus = 500;

    /**
     * Render an exception into an HTTP response.
     *
     * @access public
     * @param \think\Request $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {

//        return parent::render($request, $e);
//        $message = $e->getMessage();
        $message = $e;
        if (!empty($message)) {
            $tmpMsgArr = explode('--$_e_$--', $message);
            if (isset($tmpMsgArr[0]) && !empty($tmpMsgArr[0])) {
                $message = $tmpMsgArr[0];
            }
        }
        // 添加自定义异常处理机制
        if (method_exists($e, "getStatusCode")) {
            $httpStatus = $e->getStatusCode();
        } else {
            $httpStatus = $this->httpStatus;
        }

        return show($e->getCode(), $message, [], $httpStatus);
        // 其他错误交给系统处理
        // return parent::render($request, $e);
    }
}